package vn.com.vega.chacha.v2;

import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.music.database.DataStore;
import vn.com.vega.music.view.adapter.SongListAdapter;
import vn.com.vega.music.view.custom.BottomPlayerView;
import vn.com.vega.music.view.custom.LoadingView;
import vn.com.vega.music.view.custom.TopTaskbarView;
import vn.com.vega.music.view.custom.BottomPlayerView.ShowHideBottomPlayerListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class SongDownloadedScreen extends ActivityBase implements OnClickListener {
	private TopTaskbarView mTopTaskbarView;
	private ListView mListView;
	private SongListAdapter mAdapter;
	private DataStore dataStore;
	private LoadingView mLoadingView;
	private BottomPlayerView mBottomPlayer;
	private View mPlayerViewReplace;
	private View mPlayAllSong;
	private View mSelectSongPlay;

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		dataStore = DataStore.getInstance();
		if (dataStore.getListDownloadedSongs().size() == 0) {
			showToast("Chưa tải về bài hát nào!");
			finish();
			return;
		}

		setContentView(R.layout.local_song_screen);
		mListView = (ListView) findViewById(R.id.lv_list_song);
		mLoadingView = (LoadingView) findViewById(R.id.loadingView_loading_list_song_local);
		mLoadingView.setVisibility(View.GONE);
		mTopTaskbarView = (TopTaskbarView) findViewById(R.id.topTaskbarView_song_local);
		mTopTaskbarView.showBackButton();
		mTopTaskbarView.changeToTitleOnly("Nhạc đã tải về");
		List<Object> list = new ArrayList<Object>();
		list.addAll(dataStore.getListDownloadedSongs());
		mAdapter = new SongListAdapter(this, list, true,
				SongDownloadedScreen.class.getSimpleName());
		mListView.setAdapter(mAdapter);
		
		mSelectSongPlay = findViewById(R.id.tv_select_song_play);
		mPlayAllSong = findViewById(R.id.tv_play_all_song);
		mSelectSongPlay.setOnClickListener(this);
		mPlayAllSong.setOnClickListener(this);

		mPlayerViewReplace = findViewById(R.id.view_replace_bottom_player);
		mBottomPlayer = (BottomPlayerView) findViewById(R.id.bottomPlayerView_song_local);
		mBottomPlayer.setShowHideListener(showHideBottomPlayerListener);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mBottomPlayer.startUpdate();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mBottomPlayer.stopUpdate();
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_select_song_play:
			if (mAdapter != null && mAdapter.getCount() > 0) {
				Appinstant app = (Appinstant) getApplication();
				app.tranportString.put(SelecSongPlay.KEY_LIST_SONG_TITLE, "List song downloaded");
				mAdapter.selectSongPlay("List song downloaded");
			}
			break;
		case R.id.tv_play_all_song:
			if (mAdapter != null && mAdapter.getCount() > 0) {
				mAdapter.playAllSong("Song downloaded");
			}
			break;

		default:
			break;
		}
	}

	ShowHideBottomPlayerListener showHideBottomPlayerListener = new ShowHideBottomPlayerListener() {

		@Override
		public void onShowBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.GONE);
		}
	};

}
